var param = JSON.parse(localStorage.getItem('param'));
var productId = param.productId;
var userId = localStorage.getItem('userid');
var type = param.type;
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	//加载资讯信息，并且在资讯的阅读量上加1(销量)
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/product/getTeaZXInfo.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			productId:productId,
			userId:userId
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				$("#title").html(result.data.teaTitle);
				$("#teaSellNum").html(result.data.teaSellNum);
				$("#addTime").html(result.data.addtime);
				$("#imageId").attr("src",result.data.pic);
				$("#desception").html(result.data.desception);
				
			}else{
				showErrorMsg(result.data);
			}
		}
	});
	//初始化评论
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/comment/getCommentList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			orderId:productId,
			type:"part"
		},
		success : function(result) {
			var str = '<div class="comment-normal"><span>评价</span></div>';
			if (result.retCode == '0000') {
				$.each(result.data,function(i,n){
					var id = n.id||'';
					var userId = n.userid||'';
					var userName = n.username||'';
					var orderId = n.orderid||'';
					var pics = n.pics.split(',');
					var imgStr = '<div class="comment-img clear">';
					$.each(pics,function(j,m){
						imgStr +='<div class="img-box fl"><img src="'+m+'" alt="" /></div>';
					});
					imgStr +='</div>';
					var msg = n.msg||'';
					var addTime = n.addtime.substring(0,10)||'';
					str +='<div class="comment-list"><div class="comment-item clear"><span class="fl comment-name">'+userName+'</span><span class="fr comment-time">'+addTime+'</span></div><p class="comment-con">'+msg+'</p>'+imgStr+'</div>';
				});
				if(result.data.length>0){
					str += '<div class="comment-btn"><span>查看全部评价</span></div>';
				}
			}
			$('#commentList').html(str);
		}
	});
	/**
	 * 查看用户是否点赞
	 */
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/favour/checkFavour.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			userId:userId,
			productId:productId
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				if(result.data){
					$('#teaFavour').addClass("comment-btn-sel");
					$('.comment-btn-z').attr('markid',result.data);
				}
			}else{
				showErrorMsg(result.data);
			}
		}
	});
} 
//初始化绑定事件
function initButton(){
	$("body").on('click','.comment-btn',function(){
		localStorage.setItem('orderId',productId);
		window.location = server_url + "allComment.do";
	});
	//购买按钮
	$('body').on('click','.buy-box',function(){
		var param = {};
		param.productId = productId;
		param.type = type;//为了订单类型区分
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "buyNow.do";
	});
	//收藏
	$('.comment-btn-z').on('click', function() {
		$(this).children('p').toggleClass('collect-sel');
		var collect = $(this);
		var collectId = $(this).attr('collectid');
		if(!(collectId=="")){
			//原先是收藏的，现在点是为了取消收藏
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/user/collect/deleteCollect.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					id:collectId
				},
				success : function(result) {
					collect.attr('collectid','');
				}
			});
		}else{
			//原先没有收藏现在是在收藏
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/user/collect/addCollect.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					productId:productId,
					userId:localStorage.getItem('userid')
				},
				success : function(result) {
					collect.attr('collectid',result.data.id);
				}
			});
		}
	});
	
	//评论
	$('body').on('click','.comment-btn-l', function() {
		
		window.location = server_url + "zixunComment.do";
	});
	
	//绑定点赞
	$('body').on('click','.comment-btn-z', function() {
		$(this).children('p').toggleClass('comment-btn-sel');
		var favour = $(this);
		var markId = $(this).attr('markid');
		if(!(markId=="")){
			//原先是点过赞的，现在点是为了取消点赞
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/favour/deleteFavour.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					id:markId
				},
				success : function(result) {
					favour.attr('markid','');
				}
			});
		}else{
			//原先没有点赞现在是在点赞
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/favour/addFavour.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					markId:productId,
					userId:userId
				},
				success : function(result) {
					favour.attr('markid',result.data.id);
				}
			});
		}
	})
}